/**
  ******************************************************************************
  * @file    bsp_io.c
  * @author  Iron
  * @date    2018-07-26
  * @version v1.0
  * @brief   controler input ouput c file
  */

/** @addtogroup GROUP_BSP
  * @{
  */

/* includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "bsp_io.h"

/* private typedef -----------------------------------------------------------*/
/* private macro -------------------------------------------------------------*/

// OUTPUT
#define BSP_OUTPUT_01_PIN   GPIO_PIN_13  // PB11
#define BSP_OUTPUT_01_PORT  GPIOB

#define BSP_OUTPUT_02_PIN   GPIO_PIN_13  // PB12
#define BSP_OUTPUT_02_PORT  GPIOB

#define BSP_OUTPUT_01_GET_STATE()      HAL_GPIO_ReadPin(BSP_OUTPUT_01_PORT, BSP_OUTPUT_01_PIN)
#define BSP_OUTPUT_01_SET_STATE(state) HAL_GPIO_WritePin(BSP_OUTPUT_01_PORT, BSP_OUTPUT_01_PIN, state)

#define BSP_OUTPUT_02_GET_STATE()      HAL_GPIO_ReadPin(BSP_OUTPUT_02_PORT, BSP_OUTPUT_02_PIN)
#define BSP_OUTPUT_02_SET_STATE(state) HAL_GPIO_WritePin(BSP_OUTPUT_02_PORT, BSP_OUTPUT_02_PIN, state)

// INPUT
#define BSP_INPUT_01_PIN   GPIO_PIN_13  // PB13
#define BSP_INPUT_01_PORT  GPIOB

#define BSP_INPUT_02_PIN   GPIO_PIN_14  // PB14
#define BSP_INPUT_02_PORT  GPIOB

#define BSP_INPUT_03_PIN   GPIO_PIN_0   // PE0
#define BSP_INPUT_03_PORT  GPIOE

#define BSP_INPUT_01_STATE() HAL_GPIO_ReadPin(BSP_INPUT_01_PORT, BSP_INPUT_01_PIN)
#define BSP_INPUT_02_STATE() HAL_GPIO_ReadPin(BSP_INPUT_02_PORT, BSP_INPUT_02_PIN)
#define BSP_INPUT_03_STATE() HAL_GPIO_ReadPin(BSP_INPUT_03_PORT, BSP_INPUT_03_PIN)


#define BSP_OUTPUT_INPUT_PORT_RCC_INIT()    \
                __HAL_RCC_GPIOB_CLK_ENABLE();\
                __HAL_RCC_GPIOE_CLK_ENABLE()

/* private variables ---------------------------------------------------------*/
/* private function prototypes -----------------------------------------------*/
/* private functions ---------------------------------------------------------*/

int32_t bsp_io_get_input(int32_t num)
{
    int32_t state = 0;

    switch (num)
    {
    case IN1:
        state = BSP_INPUT_01_STATE();
        break;
    case IN2:
        state = BSP_INPUT_02_STATE();
        break;
    case IN3:
        state = BSP_INPUT_03_STATE();
        break;
    }

    return state;
}

int32_t bsp_io_get_output(int32_t num)
{
    GPIO_PinState st = GPIO_PIN_RESET;

    switch (num)
    {
    case OUT1:
        st = BSP_OUTPUT_01_GET_STATE();
        break;
    case OUT2:
        st = BSP_OUTPUT_02_GET_STATE();
        break;
    }

    return st;
}

void bsp_io_set_output(int32_t num, int32_t state)
{
    GPIO_PinState st;

    st = (state) ? GPIO_PIN_SET : GPIO_PIN_RESET;

    switch (num)
    {
    case OUT1:
        BSP_OUTPUT_01_SET_STATE(st);
        break;
    case OUT2:
        BSP_OUTPUT_02_SET_STATE(st);
        break;
    }
}

int32_t bsp_io_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    BSP_OUTPUT_INPUT_PORT_RCC_INIT();

    // INPUT
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Pin  = BSP_INPUT_01_PIN;
    HAL_GPIO_Init(BSP_INPUT_01_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin  = BSP_INPUT_02_PIN;
    HAL_GPIO_Init(BSP_INPUT_02_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin  = BSP_INPUT_03_PIN;
    HAL_GPIO_Init(BSP_INPUT_03_PORT, &GPIO_InitStruct);

    // OUPUT
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

    GPIO_InitStruct.Pin  = BSP_OUTPUT_01_PIN;
    HAL_GPIO_Init(BSP_OUTPUT_01_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin  = BSP_OUTPUT_02_PIN;
    HAL_GPIO_Init(BSP_OUTPUT_02_PORT, &GPIO_InitStruct);

    return 0;
}

/**
  * @}
  */

/******************* (C)COPYRIGHT 2018 ***** END OF FILE *********************/
